using System;

namespace Nop.Core.Caching
{
    /// <summary>
    /// Extensions
    /// </summary>
    public static class CacheExtensions
    {
        public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire,int cacheTime = 600)
        {
            return Get(cacheManager, key, cacheTime, acquire);
        }

        public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, Func<T> acquire)
        {
			return Get(cacheManager, key, cacheTime, true, acquire);           
        }

		public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, bool ignoreNullValue, Func<T> acquire)
		{
			if (cacheManager.IsSet(key))
			{
				return cacheManager.Get<T>(key);
			}
			else
			{
				var result = acquire();
				if (ignoreNullValue && result == null)
				{
					return result;
				}					
				cacheManager.Set(key, result, cacheTime);
				return result;
			}
		}
    }
}
